package org.example.flux;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Sinks;

import javax.swing.*;
import java.awt.*;

/**
 * 插槽
 */
public class Demo1 {

    public static void main(String[] args) throws InterruptedException {
        //插槽
        Sinks.Many<Integer> sink = Sinks.many().unicast().onBackpressureBuffer();
        //流
        Flux<Integer> flux = sink.asFlux();
        //窗口
        final JLabel label = new JLabel("");
        JFrame frame = new JFrame("Reactor Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);
        frame.add(label, BorderLayout.CENTER);
        frame.setVisible(true);
        //订阅
        flux.subscribe(s -> label.setText(String.valueOf(s)));
        //发布
        for (int i = 10; i >= 0; i--) {
            Thread.sleep(1000);
            sink.emitNext(i, Sinks.EmitFailureHandler.FAIL_FAST);
        }
    }
}
